<%@ page import="com.afc.struts.bustle.product.ProductForm" %>
<%@ page import="com.afc.struts.bustle.product.ProductForm.DefaultProductForm" %>
<%@ page import="com.afc.struts.bustle.model.ProductDto" %>
<%@ page import="com.afc.struts.bustle.model.ProviderDto" %>
<%@ page import="com.afc.struts.bustle.model.ProductDto.ProductType" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.afc.struts.utils.DateUtils" %>

<%
  String error = (String)request.getAttribute("error");
  ProductForm form = (ProductForm)request.getAttribute("form");
  if (form == null)
  {
    form = new DefaultProductForm();
  }
%>

<html>
<head>
<script type="text/javascript">
function submitForm(anActionUrl, aFormElement)
{
  aFormElement.action = anActionUrl;
  aFormElement.submit();
}
</script>
</head>

<body>
  <a href="welcome.jsp">Welcome</a>
  <form method="post">
    Id : <input type="text" name="id" value="<%= form.getId() %>" readonly /><br />
    Type : <select name="type" attribute-type="com.afc.struts.bustle.model.ProductDto.ProductType">
      <option value="" <%= form.getType() == null ? "selected" : "" %>></option>
      <%
        ProductType[] types = ProductType.values();
        for (int i = 0; i < types.length; i++)
        {
      %>
          <option value=<%= types[i].name() %> <%= form.getType() == types[i] ? "selected" : "" %>><%= types[i] %></option>
      <%
        }
      %>
    </select><br />
    Name : <input type="text" attribute-type="java.lang.String" name="name" value='<%= form.getName() != null ? form.getName() : "" %>' /><br />
    Price : <input type="text" attribute-type="double" name="price" value='<%= form.getPrice() %>' /><br />
    Description : <input type="text" attribute-type="java.lang.String" name="description" value='<%= form.getDescription() != null ? form.getDescription() : "" %>' /><br />
    CreationDate : <input type="text" attribute-type="java.util.date" name="creationDate" value='<%= form.getCreationDate() != null ? DateUtils.format(form.getCreationDate()) : "" %>' /><br />
    LimitedDate : <input type="text" attribute-type="java.util.date" name="limitedDate" value='<%= form.getLimitedDate() != null ? DateUtils.format(form.getLimitedDate()) : "" %>' /><br />
    Image : <input type="text" attribute-type="java.lang.String" name="image" value='<%= form.getImage() != null ? form.getImage() : "" %>' /><br />
    Provider : <select attribute-type="com.afc.struts.bustle.model.ProviderDto" name="provider">
      <%
        Iterator iterator = form.getPossibleProviders().iterator();
        for (int i = 0; iterator.hasNext(); i++)
        {
          ProviderDto provider = (ProviderDto)iterator.next();
      %>
          <option value=<%= i %> <%= (provider != null && form.getProvider() != null && provider.getIdProvider() == form.getProvider().getIdProvider()) || provider == form.getProvider() ? "selected" : "" %>><%= provider != null ? provider : "" %></option>
      <%
        }
      %>
    </select><br />
    <input type="button" value="Update" onclick='submitForm("productUpdate.ok.do", this.form)' />
    <a href="products.jsp"><input type="button" value="Cancel" /></a>
    <label><%= error != null ? error : "" %></label>
  </form>

</body>

</html>